﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DiscoveryLogic.Common.Numeric
{
    /// <summary>Abstract base class for resizable lists holding <code>int</code> elements; abstract.
    /// First see the <a href="package-summary.html">package summary</a> and javadoc <a href="package-tree.html">tree view</a> to get the broad picture.
    /// </summary>
    [Serializable]
    public abstract class AbstractIntList : AbstractList, IntBufferConsumer, System.ICloneable
    {
        /// <summary> Sets the size of the receiver without modifying it otherwise.
        /// This method should not release or allocate new memory but simply set some instance variable like <tt>size</tt>.
        /// 
        /// If your subclass overrides and delegates size changing methods to some other object,
        /// you must make sure that those overriding methods not only update the size of the delegate but also of this class.
        /// For example:
        /// public DatabaseList extends AbstractIntList {
        /// ...
        /// public void removeFromTo(int from,int to) {
        /// myDatabase.removeFromTo(from,to);
        /// this.setSizeRaw(size-(to-from+1));
        /// }
        /// }
        /// </summary>
        virtual protected internal int SizeRaw
        {
            set
            {
                size_Renamed_Field = value;
            }

        }
        /// <summary> The size of the list.
        /// This is a READ_ONLY variable for all methods but setSizeRaw(int newSize) !!!
        /// If you violate this principle in subclasses, you should exactly know what you are doing.
        /// </summary>
        /// <serial>
        /// </serial>
        protected internal int size_Renamed_Field;
        /// <summary> Makes this class non instantiable, but still let's others inherit from it.</summary>
        protected internal AbstractIntList()
        {
        }
        /// <summary> Appends the specified element to the end of this list.
        /// 
        /// </summary>
        /// <param name="element">element to be appended to this list.
        /// </param>
        public virtual void add(int element)
        {
            beforeInsert(size_Renamed_Field, element);
        }
        /// <summary> Appends all elements of the specified list to the receiver.</summary>
        /// <param name="list">the list of which all elements shall be appended.
        /// </param>
        public virtual void addAllOf(IntArrayList other)
        {
            addAllOfFromTo(other, 0, other.size() - 1);
        }
        /// <summary> Appends the part of the specified list between <code>from</code> (inclusive) and <code>to</code> (inclusive) to the receiver.
        /// 
        /// </summary>
        /// <param name="other">the list to be added to the receiver.
        /// </param>
        /// <param name="from">the index of the first element to be appended (inclusive).
        /// </param>
        /// <param name="to">the index of the last element to be appended (inclusive).
        /// </param>
        /// <exception cref="IndexOutOfBoundsException">index is out of range (<tt>other.size()&gt;0 && (from&lt;0 || from&gt;to || to&gt;=other.size())</tt>).
        /// </exception>
        public virtual void addAllOfFromTo(AbstractIntList other, int from, int to)
        {
            beforeInsertAllOfFromTo(size_Renamed_Field, other, from, to);
        }
        /// <summary> Inserts the specified element before the specified position into the receiver. 
        /// Shifts the element currently at that position (if any) and
        /// any subsequent elements to the right.
        /// 
        /// </summary>
        /// <param name="index">index before which the specified element is to be inserted (must be in [0,size]).
        /// </param>
        /// <param name="element">element to be inserted.
        /// </param>
        /// <exception cref="IndexOutOfBoundsException">index is out of range (<tt>index &lt; 0 || index &gt; size()</tt>).
        /// </exception>
        public virtual void beforeInsert(int index, int element)
        {
            beforeInsertDummies(index, 1);
            set_Renamed(index, element);
        }
        /// <summary> Inserts the part of the specified list between <code>otherFrom</code> (inclusive) and <code>otherTo</code> (inclusive) before the specified position into the receiver. 
        /// Shifts the element currently at that position (if any) and
        /// any subsequent elements to the right.
        /// 
        /// </summary>
        /// <param name="index">index before which to insert first element from the specified list (must be in [0,size])..
        /// </param>
        /// <param name="other">list of which a part is to be inserted into the receiver.
        /// </param>
        /// <param name="from">the index of the first element to be inserted (inclusive).
        /// </param>
        /// <param name="to">the index of the last element to be inserted (inclusive).
        /// </param>
        /// <exception cref="IndexOutOfBoundsException">index is out of range (<tt>other.size()&gt;0 && (from&lt;0 || from&gt;to || to&gt;=other.size())</tt>).
        /// </exception>
        /// <exception cref="IndexOutOfBoundsException">index is out of range (<tt>index &lt; 0 || index &gt; size()</tt>).
        /// </exception>
        public virtual void beforeInsertAllOfFromTo(int index, AbstractIntList other, int from, int to)
        {
            int length = to - from + 1;
            this.beforeInsertDummies(index, length);
            this.replaceFromToWithFrom(index, index + length - 1, other, from);
        }
        /// <summary> Inserts <tt>length</tt> dummy elements before the specified position into the receiver. 
        /// Shifts the element currently at that position (if any) and
        /// any subsequent elements to the right.
        /// <b>This method must set the new size to be <tt>size()+length</tt>.
        /// 
        /// </summary>
        /// <param name="index">index before which to insert dummy elements (must be in [0,size])..
        /// </param>
        /// <param name="length">number of dummy elements to be inserted.
        /// </param>
        /// <throws>  IndexOutOfBoundsException if <tt>index &lt; 0 || index &gt; size()</tt>. </throws>
        protected internal override void beforeInsertDummies(int index, int length)
        {
            if (index > size_Renamed_Field || index < 0)
                throw new System.IndexOutOfRangeException("Index: " + index + ", Size: " + size_Renamed_Field);
            if (length > 0)
            {
                ensureCapacity(size_Renamed_Field + length);
                SizeRaw = size_Renamed_Field + length;
                replaceFromToWithFrom(index + length, size_Renamed_Field - 1, this, index);
            }
        }
        /// <summary> Searches the receiver for the specified value using
        /// the binary search algorithm.  The receiver must <strong>must</strong> be
        /// sorted (as by the sort method) prior to making this call.  If
        /// it is not sorted, the results are undefined: in particular, the call
        /// may enter an infinite loop.  If the receiver contains multiple elements
        /// equal to the specified object, there is no guarantee which instance
        /// will be found.
        /// 
        /// </summary>
        /// <param name="key">the value to be searched for.
        /// </param>
        /// <returns> index of the search key, if it is contained in the receiver;
        /// otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The <i>insertion
        /// point</i> is defined as the the point at which the value would
        /// be inserted into the receiver: the index of the first
        /// element greater than the key, or <tt>receiver.size()</tt>, if all
        /// elements in the receiver are less than the specified key.  Note
        /// that this guarantees that the return value will be &gt;= 0 if
        /// and only if the key is found.
        /// </returns>
        /// <seealso cref="java.util.Arrays">
        /// </seealso>
        public virtual int binarySearch(int key)
        {
            return this.binarySearchFromTo(key, 0, size_Renamed_Field - 1);
        }
        /// <summary> Searches the receiver for the specified value using
        /// the binary search algorithm.  The receiver must <strong>must</strong> be
        /// sorted (as by the sort method) prior to making this call.  If
        /// it is not sorted, the results are undefined: in particular, the call
        /// may enter an infinite loop.  If the receiver contains multiple elements
        /// equal to the specified object, there is no guarantee which instance
        /// will be found.
        /// 
        /// </summary>
        /// <param name="key">the value to be searched for.
        /// </param>
        /// <param name="from">the leftmost search position, inclusive.
        /// </param>
        /// <param name="to">the rightmost search position, inclusive.
        /// </param>
        /// <returns> index of the search key, if it is contained in the receiver;
        /// otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The <i>insertion
        /// point</i> is defined as the the point at which the value would
        /// be inserted into the receiver: the index of the first
        /// element greater than the key, or <tt>receiver.size()</tt>, if all
        /// elements in the receiver are less than the specified key.  Note
        /// that this guarantees that the return value will be &gt;= 0 if
        /// and only if the key is found.
        /// </returns>
        /// <seealso cref="java.util.Arrays">
        /// </seealso>
        public virtual int binarySearchFromTo(int key, int from, int to)
        {
            int low = from;
            int high = to;
            while (low <= high)
            {
                int mid = (low + high) / 2;
                int midVal = get_Renamed(mid);

                if (midVal < key)
                    low = mid + 1;
                else if (midVal > key)
                    high = mid - 1;
                else
                    return mid; // key found
            }
            return -(low + 1); // key not found.
        }
        /// <summary> Returns a deep copy of the receiver. 
        /// 
        /// </summary>
        /// <returns>  a deep copy of the receiver.
        /// </returns>
        public override System.Object Clone()
        {
            return partFromTo(0, size_Renamed_Field - 1);
        }
        /// <summary> Returns true if the receiver contains the specified element.
        /// 
        /// </summary>
        /// <param name="element">element whose presence in the receiver is to be tested.
        /// </param>
        public virtual bool contains(int elem)
        {
            return indexOfFromTo(elem, 0, size_Renamed_Field - 1) >= 0;
        }
        /// <summary> Deletes the first element from the receiver that is identical to the specified element.
        /// Does nothing, if no such matching element is contained.
        /// 
        /// </summary>
        /// <param name="element">the element to be deleted.
        /// </param>
        public virtual void delete(int element)
        {
            int index = indexOfFromTo(element, 0, size_Renamed_Field - 1);
            if (index >= 0)
                remove(index);
        }
        /// <summary> Returns the elements currently stored, possibly including invalid elements between size and capacity.
        /// 
        /// <b>WARNING:</b> For efficiency reasons and to keep memory usage low, this method may decide <b>not to copy the array</b>.
        /// So if subsequently you modify the returned array directly via the [] operator, be sure you know what you're doing.
        /// 
        /// </summary>
        /// <returns> the elements currently stored.
        /// </returns>
        public virtual int[] elements()
        {
            int[] myElements = new int[size_Renamed_Field];
            for (int i = size_Renamed_Field; --i >= 0; )
                myElements[i] = getQuick(i);
            return myElements;
        }
        /// <summary> Sets the receiver's elements to be the specified array.
        /// The size and capacity of the list is the length of the array.
        /// <b>WARNING:</b> For efficiency reasons and to keep memory usage low, this method may decide <b>not to copy the array</b>.
        /// So if subsequently you modify the returned array directly via the [] operator, be sure you know what you're doing.
        /// 
        /// </summary>
        /// <param name="elements">the new elements to be stored.
        /// </param>
        /// <returns> the receiver itself.
        /// </returns>
        public virtual AbstractIntList elements(int[] elements)
        {
            clear();
            addAllOfFromTo(new IntArrayList(elements), 0, elements.Length - 1);
            return this;
        }
        /// <summary> Ensures that the receiver can hold at least the specified number of elements without needing to allocate new internal memory.
        /// If necessary, allocates new internal memory and increases the capacity of the receiver.
        /// 
        /// </summary>
        /// <param name="minCapacity">  the desired minimum capacity.
        /// </param>
        public abstract void ensureCapacity(int minCapacity);
        /// <summary> Compares the specified Object with the receiver.  
        /// Returns true if and only if the specified Object is also an ArrayList of the same type, both Lists have the
        /// same size, and all corresponding pairs of elements in the two Lists are identical.
        /// In other words, two Lists are defined to be equal if they contain the
        /// same elements in the same order.
        /// 
        /// </summary>
        /// <param name="otherObj">the Object to be compared for equality with the receiver.
        /// </param>
        /// <returns> true if the specified Object is equal to the receiver.
        /// </returns>
        public override bool Equals(System.Object otherObj)
        {
            //delta
            if (!(otherObj is AbstractIntList))
            {
                return false;
            }
            if (this == otherObj)
                return true;
            if (otherObj == null)
                return false;
            AbstractIntList other = (AbstractIntList)otherObj;
            if (size() != other.size())
                return false;

            for (int i = size(); --i >= 0; )
            {
                if (getQuick(i) != other.getQuick(i))
                    return false;
            }
            return true;
        }
        /// <summary> Sets the specified range of elements in the specified array to the specified value.
        /// 
        /// </summary>
        /// <param name="from">the index of the first element (inclusive) to be filled with the specified value.
        /// </param>
        /// <param name="to">the index of the last element (inclusive) to be filled with the specified value.
        /// </param>
        /// <param name="val">the value to be stored in the specified elements of the receiver.
        /// </param>
        public virtual void fillFromToWith(int from, int to, int val)
        {
            checkRangeFromTo(from, to, this.size_Renamed_Field);
            for (int i = from; i <= to; )
                setQuick(i++, val);
        }
        /// <summary> Applies a procedure to each element of the receiver, if any.
        /// Starts at index 0, moving rightwards.
        /// </summary>
        /// <param name="procedure">   the procedure to be applied. Stops iteration if the procedure returns <tt>false</tt>, otherwise continues. 
        /// </param>
        /// <returns> <tt>false</tt> if the procedure stopped before all elements where iterated over, <tt>true</tt> otherwise. 
        /// </returns>
        public virtual bool forEach(IntProcedure procedure)
        {
            for (int i = 0; i < size_Renamed_Field; )
                if (!procedure.apply(get_Renamed(i++)))
                    return false;
            return true;
        }
        /// <summary> Returns the element at the specified position in the receiver.
        /// 
        /// </summary>
        /// <param name="index">index of element to return.
        /// </param>
        /// <exception cref="IndexOutOfBoundsException">index is out of range (index
        /// &lt; 0 || index &gt;= size()).
        /// </exception>
        public virtual int get_Renamed(int index)
        {
            if (index >= size_Renamed_Field || index < 0)
                throw new System.IndexOutOfRangeException("Index: " + index + ", Size: " + size_Renamed_Field);
            return getQuick(index);
        }
        /// <summary> Returns the element at the specified position in the receiver; <b>WARNING:</b> Does not check preconditions. 
        /// Provided with invalid parameters this method may return invalid elements without throwing any exception!
        /// <b>You should only use this method when you are absolutely sure that the index is within bounds.</b>
        /// Precondition (unchecked): <tt>index &gt;= 0 && index &lt; size()</tt>.
        /// 
        /// This method is normally only used internally in large loops where bounds are explicitly checked before the loop and need no be rechecked within the loop.
        /// However, when desperately, you can give this method <tt>public</tt> visibility in subclasses.
        /// 
        /// </summary>
        /// <param name="index">index of element to return.
        /// </param>
        //UPGRADE_NOTE: Access modifiers of method 'getQuick' were changed to 'public'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1204'"
        public abstract int getQuick(int index);
        /// <summary> Returns the index of the first occurrence of the specified
        /// element. Returns <code>-1</code> if the receiver does not contain this element.
        /// 
        /// </summary>
        /// <param name="element">  the element to be searched for.
        /// </param>
        /// <returns>  the index of the first occurrence of the element in the receiver; returns <code>-1</code> if the element is not found.
        /// </returns>
        public virtual int indexOf(int element)
        {
            //delta
            return indexOfFromTo(element, 0, size_Renamed_Field - 1);
        }
        /// <summary> Returns the index of the first occurrence of the specified
        /// element. Returns <code>-1</code> if the receiver does not contain this element.
        /// Searches between <code>from</code>, inclusive and <code>to</code>, inclusive.
        /// Tests for identity.
        /// 
        /// </summary>
        /// <param name="element">element to search for.
        /// </param>
        /// <param name="from">the leftmost search position, inclusive.
        /// </param>
        /// <param name="to">the rightmost search position, inclusive.
        /// </param>
        /// <returns>  the index of the first occurrence of the element in the receiver; returns <code>-1</code> if the element is not found.
        /// </returns>
        /// <exception cref="IndexOutOfBoundsException">index is out of range (<tt>size()&gt;0 && (from&lt;0 || from&gt;to || to&gt;=size())</tt>).
        /// </exception>
        public virtual int indexOfFromTo(int element, int from, int to)
        {
            checkRangeFromTo(from, to, size_Renamed_Field);

            for (int i = from; i <= to; i++)
            {
                if (element == getQuick(i))
                    return i; //found
            }
            return -1; //not found
        }
        /// <summary> Returns the index of the last occurrence of the specified
        /// element. Returns <code>-1</code> if the receiver does not contain this element.
        /// 
        /// </summary>
        /// <param name="element">  the element to be searched for.
        /// </param>
        /// <returns>  the index of the last occurrence of the element in the receiver; returns <code>-1</code> if the element is not found.
        /// </returns>
        public virtual int lastIndexOf(int element)
        {
            return lastIndexOfFromTo(element, 0, size_Renamed_Field - 1);
        }
        /// <summary> Returns the index of the last occurrence of the specified
        /// element. Returns <code>-1</code> if the receiver does not contain this element.
        /// Searches beginning at <code>to</code>, inclusive until <code>from</code>, inclusive.
        /// Tests for identity.
        /// 
        /// </summary>
        /// <param name="element">element to search for.
        /// </param>
        /// <param name="from">the leftmost search position, inclusive.
        /// </param>
        /// <param name="to">the rightmost search position, inclusive.
        /// </param>
        /// <returns>  the index of the last occurrence of the element in the receiver; returns <code>-1</code> if the element is not found.
        /// </returns>
        /// <exception cref="IndexOutOfBoundsException">index is out of range (<tt>size()&gt;0 && (from&lt;0 || from&gt;to || to&gt;=size())</tt>).
        /// </exception>
        public virtual int lastIndexOfFromTo(int element, int from, int to)
        {
            checkRangeFromTo(from, to, size());

            for (int i = to; i >= from; i--)
            {
                if (element == getQuick(i))
                    return i; //found
            }
            return -1; //not found
        }
        /// <summary> Sorts the specified range of the receiver into ascending order. 
        /// 
        /// The sorting algorithm is a modified mergesort (in which the merge is
        /// omitted if the highest element in the low sublist is less than the
        /// lowest element in the high sublist).  This algorithm offers guaranteed
        /// n*log(n) performance, and can approach linear performance on nearly
        /// sorted lists.
        /// 
        /// <p><b>You should never call this method unless you are sure that this particular sorting algorithm is the right one for your data set.</b>
        /// It is generally better to call <tt>sort()</tt> or <tt>sortFromTo(...)</tt> instead, because those methods automatically choose the best sorting algorithm.
        /// 
        /// </summary>
        /// <param name="from">the index of the first element (inclusive) to be sorted.
        /// </param>
        /// <param name="to">the index of the last element (inclusive) to be sorted.
        /// </param>
        /// <exception cref="IndexOutOfBoundsException">index is out of range (<tt>size()&gt;0 && (from&lt;0 || from&gt;to || to&gt;=size())</tt>).
        /// </exception>
        public override void mergeSortFromTo(int from, int to)
        {
            int mySize = size();
            checkRangeFromTo(from, to, mySize);

            int[] myElements = elements();
            Sorting.mergeSort(myElements, from, to + 1);
            elements(myElements);
            SizeRaw = mySize;
        }
        /// <summary> Sorts the receiver according
        /// to the order induced by the specified comparator.  All elements in the
        /// range must be <i>mutually comparable</i> by the specified comparator
        /// (that is, <tt>c.compare(e1, e2)</tt> must not throw a
        /// <tt>ClassCastException</tt> for any elements <tt>e1</tt> and
        /// <tt>e2</tt> in the range).<p>
        /// 
        /// This sort is guaranteed to be <i>stable</i>:  equal elements will
        /// not be reordered as a result of the sort.<p>
        /// 
        /// The sorting algorithm is a modified mergesort (in which the merge is
        /// omitted if the highest element in the low sublist is less than the
        /// lowest element in the high sublist).  This algorithm offers guaranteed
        /// n*log(n) performance, and can approach linear performance on nearly
        /// sorted lists.
        /// 
        /// </summary>
        /// <param name="from">the index of the first element (inclusive) to be
        /// sorted.
        /// </param>
        /// <param name="to">the index of the last element (inclusive) to be sorted.
        /// </param>
        /// <param name="c">the comparator to determine the order of the receiver.
        /// </param>
        /// <throws>  ClassCastException if the array contains elements that are not </throws>
        /// <summary>	       <i>mutually comparable</i> using the specified comparator.
        /// </summary>
        /// <throws>  IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt> </throws>
        /// <throws>  ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or </throws>
        /// <summary>	       <tt>toIndex &gt; a.length</tt>
        /// </summary>
        /// <seealso cref="Comparator">
        /// </seealso>
        /// <exception cref="IndexOutOfBoundsException">index is out of range (<tt>size()&gt;0 && (from&lt;0 || from&gt;to || to&gt;=size())</tt>).
        /// </exception>
        public virtual void mergeSortFromTo(int from, int to, IntComparator c)
        {
            int mySize = size();
            checkRangeFromTo(from, to, mySize);

            int[] myElements = elements();
            Sorting.mergeSort(myElements, from, to + 1, c);
            elements(myElements);
            SizeRaw = mySize;
        }
        /// <summary> Returns a new list of the part of the receiver between <code>from</code>, inclusive, and <code>to</code>, inclusive.</summary>
        /// <param name="from">the index of the first element (inclusive).
        /// </param>
        /// <param name="to">the index of the last element (inclusive).
        /// </param>
        /// <returns> a new list
        /// </returns>
        /// <exception cref="IndexOutOfBoundsException">index is out of range (<tt>size()&gt;0 && (from&lt;0 || from&gt;to || to&gt;=size())</tt>).
        /// </exception>
        public virtual AbstractIntList partFromTo(int from, int to)
        {
            checkRangeFromTo(from, to, size_Renamed_Field);

            int length = to - from + 1;
            IntArrayList part = new IntArrayList(length);
            part.addAllOfFromTo(this, from, to);
            return part;
        }
        /// <summary> Sorts the specified range of the receiver into
        /// ascending numerical order.  The sorting algorithm is a tuned quicksort,
        /// adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a
        /// Sort Function", Software-Practice and Experience, Vol. 23(11)
        /// P. 1249-1265 (November 1993).  This algorithm offers n*log(n)
        /// performance on many data sets that cause other quicksorts to degrade to
        /// quadratic performance.
        /// 
        /// <p><b>You should never call this method unless you are sure that this particular sorting algorithm is the right one for your data set.</b>
        /// It is generally better to call <tt>sort()</tt> or <tt>sortFromTo(...)</tt> instead, because those methods automatically choose the best sorting algorithm.
        /// 
        /// </summary>
        /// <param name="from">the index of the first element (inclusive) to be sorted.
        /// </param>
        /// <param name="to">the index of the last element (inclusive) to be sorted.
        /// </param>
        /// <exception cref="IndexOutOfBoundsException">index is out of range (<tt>size()&gt;0 && (from&lt;0 || from&gt;to || to&gt;=size())</tt>).
        /// </exception>
        public override void quickSortFromTo(int from, int to)
        {
            int mySize = size();
            checkRangeFromTo(from, to, mySize);

            int[] myElements = elements();
            System.Array.Sort(myElements, from, to + 1 - from);
            //cern.colt.Sorting.mergeSort(myElements, from, to+1); // TODO just for debugging
            elements(myElements);
            SizeRaw = mySize;
        }
        /// <summary> Sorts the receiver according
        /// to the order induced by the specified comparator.  All elements in the
        /// range must be <i>mutually comparable</i> by the specified comparator
        /// (that is, <tt>c.compare(e1, e2)</tt> must not throw a
        /// <tt>ClassCastException</tt> for any elements <tt>e1</tt> and
        /// <tt>e2</tt> in the range).<p>
        /// 
        /// The sorting algorithm is a tuned quicksort,
        /// adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a
        /// Sort Function", Software-Practice and Experience, Vol. 23(11)
        /// P. 1249-1265 (November 1993).  This algorithm offers n*log(n)
        /// performance on many data sets that cause other quicksorts to degrade to
        /// quadratic performance.
        /// 
        /// </summary>
        /// <param name="from">the index of the first element (inclusive) to be
        /// sorted.
        /// </param>
        /// <param name="to">the index of the last element (inclusive) to be sorted.
        /// </param>
        /// <param name="c">the comparator to determine the order of the receiver.
        /// </param>
        /// <throws>  ClassCastException if the array contains elements that are not </throws>
        /// <summary>	       <i>mutually comparable</i> using the specified comparator.
        /// </summary>
        /// <throws>  IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt> </throws>
        /// <throws>  ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or </throws>
        /// <summary>	       <tt>toIndex &gt; a.length</tt>
        /// </summary>
        /// <seealso cref="Comparator">
        /// </seealso>
        /// <exception cref="IndexOutOfBoundsException">index is out of range (<tt>size()&gt;0 && (from&lt;0 || from&gt;to || to&gt;=size())</tt>).
        /// </exception>
        public virtual void quickSortFromTo(int from, int to, IntComparator c)
        {
            int mySize = size();
            checkRangeFromTo(from, to, mySize);

            int[] myElements = elements();
            Sorting.quickSort(myElements, from, to + 1, c);
            elements(myElements);
            SizeRaw = mySize;
        }
        /// <summary> Removes from the receiver all elements that are contained in the specified list.
        /// Tests for identity.
        /// 
        /// </summary>
        /// <param name="other">the other list.
        /// </param>
        /// <returns> <code>true</code> if the receiver changed as a result of the call.
        /// </returns>
        public virtual bool removeAll(AbstractIntList other)
        {
            if (other.size() == 0)
                return false; //nothing to do
            int limit = other.size() - 1;
            int j = 0;

            for (int i = 0; i < size_Renamed_Field; i++)
            {
                if (other.indexOfFromTo(getQuick(i), 0, limit) < 0)
                    setQuick(j++, getQuick(i));
            }

            bool modified = (j != size_Renamed_Field);
            Size = j;
            return modified;
        }
        /// <summary> Removes from the receiver all elements whose index is between
        /// <code>from</code>, inclusive and <code>to</code>, inclusive.  Shifts any succeeding
        /// elements to the left (reduces their index).
        /// This call shortens the list by <tt>(to - from + 1)</tt> elements.
        /// 
        /// </summary>
        /// <param name="from">index of first element to be removed.
        /// </param>
        /// <param name="to">index of last element to be removed.
        /// </param>
        /// <exception cref="IndexOutOfBoundsException">index is out of range (<tt>size()&gt;0 && (from&lt;0 || from&gt;to || to&gt;=size())</tt>).
        /// </exception>
        public override void removeFromTo(int from, int to)
        {
            checkRangeFromTo(from, to, size_Renamed_Field);
            int numMoved = size_Renamed_Field - to - 1;
            if (numMoved > 0)
            {
                replaceFromToWithFrom(from, from - 1 + numMoved, this, to + 1);
                //fillFromToWith(from+numMoved, size-1, 0.0f); //delta
            }
            int width = to - from + 1;
            if (width > 0)
                SizeRaw = size_Renamed_Field - width;
        }
        /// <summary> Replaces a number of elements in the receiver with the same number of elements of another list.
        /// Replaces elements in the receiver, between <code>from</code> (inclusive) and <code>to</code> (inclusive),
        /// with elements of <code>other</code>, starting from <code>otherFrom</code> (inclusive).
        /// 
        /// </summary>
        /// <param name="from">the position of the first element to be replaced in the receiver
        /// </param>
        /// <param name="to">the position of the last element to be replaced in the receiver
        /// </param>
        /// <param name="other">list holding elements to be copied into the receiver.
        /// </param>
        /// <param name="otherFrom">position of first element within other list to be copied.
        /// </param>
        public virtual void replaceFromToWithFrom(int from, int to, AbstractIntList other, int otherFrom)
        {
            int length = to - from + 1;
            if (length > 0)
            {
                checkRangeFromTo(from, to, size());
                checkRangeFromTo(otherFrom, otherFrom + length - 1, other.size());

                // unambiguous copy (it may hold other==this)
                if (from <= otherFrom)
                {
                    for (; --length >= 0; )
                        setQuick(from++, other.getQuick(otherFrom++));
                }
                else
                {
                    int otherTo = otherFrom + length - 1;
                    for (; --length >= 0; )
                        setQuick(to--, other.getQuick(otherTo--));
                }
            }
        }
        /// <summary> Replaces the part between <code>from</code> (inclusive) and <code>to</code> (inclusive) with the other list's
        /// part between <code>otherFrom</code> and <code>otherTo</code>. 
        /// Powerful (and tricky) method!
        /// Both parts need not be of the same size (part A can both be smaller or larger than part B).
        /// Parts may overlap.
        /// Receiver and other list may (but most not) be identical.
        /// If <code>from &gt; to</code>, then inserts other part before <code>from</code>.
        /// 
        /// </summary>
        /// <param name="from">the first element of the receiver (inclusive)
        /// </param>
        /// <param name="to">the last element of the receiver (inclusive)
        /// </param>
        /// <param name="other">the other list (may be identical with receiver)
        /// </param>
        /// <param name="otherFrom">the first element of the other list (inclusive)
        /// </param>
        /// <param name="otherTo">the last element of the other list (inclusive)
        /// 
        /// <p><b>Examples:</b><pre>
        /// a=[0, 1, 2, 3, 4, 5, 6, 7]
        /// b=[50, 60, 70, 80, 90]
        /// a.R(...)=a.replaceFromToWithFromTo(...)
        /// 
        /// a.R(3,5,b,0,4)-->[0, 1, 2, 50, 60, 70, 80, 90, 6, 7]
        /// a.R(1,6,b,0,4)-->[0, 50, 60, 70, 80, 90, 7]
        /// a.R(0,6,b,0,4)-->[50, 60, 70, 80, 90, 7]
        /// a.R(3,5,b,1,2)-->[0, 1, 2, 60, 70, 6, 7]
        /// a.R(1,6,b,1,2)-->[0, 60, 70, 7]
        /// a.R(0,6,b,1,2)-->[60, 70, 7]
        /// a.R(5,3,b,0,4)-->[0, 1, 2, 3, 4, 50, 60, 70, 80, 90, 5, 6, 7]
        /// a.R(5,0,b,0,4)-->[0, 1, 2, 3, 4, 50, 60, 70, 80, 90, 5, 6, 7]
        /// a.R(5,3,b,1,2)-->[0, 1, 2, 3, 4, 60, 70, 5, 6, 7]
        /// a.R(5,0,b,1,2)-->[0, 1, 2, 3, 4, 60, 70, 5, 6, 7]
        /// 
        /// Extreme cases:
        /// a.R(5,3,b,0,0)-->[0, 1, 2, 3, 4, 50, 5, 6, 7]
        /// a.R(5,3,b,4,4)-->[0, 1, 2, 3, 4, 90, 5, 6, 7]
        /// a.R(3,5,a,0,1)-->[0, 1, 2, 0, 1, 6, 7]
        /// a.R(3,5,a,3,5)-->[0, 1, 2, 3, 4, 5, 6, 7]
        /// a.R(3,5,a,4,4)-->[0, 1, 2, 4, 6, 7]
        /// a.R(5,3,a,0,4)-->[0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 6, 7]
        /// a.R(0,-1,b,0,4)-->[50, 60, 70, 80, 90, 0, 1, 2, 3, 4, 5, 6, 7]
        /// a.R(0,-1,a,0,4)-->[0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 6, 7]
        /// a.R(8,0,a,0,4)-->[0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4]
        /// </pre>
        /// </param>
        public virtual void replaceFromToWithFromTo(int from, int to, AbstractIntList other, int otherFrom, int otherTo)
        {
            if (otherFrom > otherTo)
            {
                throw new System.IndexOutOfRangeException("otherFrom: " + otherFrom + ", otherTo: " + otherTo);
            }

            if (this == other && to - from != otherTo - otherFrom)
            {
                // avoid stumbling over my own feet
                replaceFromToWithFromTo(from, to, partFromTo(otherFrom, otherTo), 0, otherTo - otherFrom);
                return;
            }

            int length = otherTo - otherFrom + 1;
            int diff = length;
            int theLast = from - 1;

            if (to >= from)
            {
                diff -= (to - from + 1);
                theLast = to;
            }

            if (diff > 0)
            {
                beforeInsertDummies(theLast + 1, diff);
            }
            else
            {
                if (diff < 0)
                {
                    removeFromTo(theLast + diff, theLast - 1);
                }
            }

            if (length > 0)
            {
                replaceFromToWithFrom(from, from + length - 1, other, otherFrom);
            }
        }
        /// <summary> Replaces the part of the receiver starting at <code>from</code> (inclusive) with all the elements of the specified collection.
        /// Does not alter the size of the receiver.
        /// Replaces exactly <tt>Math.max(0,Math.min(size()-from, other.size()))</tt> elements.
        /// 
        /// </summary>
        /// <param name="from">the index at which to copy the first element from the specified collection.
        /// </param>
        /// <param name="other">Collection to replace part of the receiver
        /// </param>
        /// <exception cref="IndexOutOfBoundsException">index is out of range (index &lt; 0 || index &gt;= size()).
        /// </exception>
        public override void replaceFromWith(int from, System.Collections.ICollection other)
        {
            checkRange(from, size());
            System.Collections.IEnumerator e = other.GetEnumerator();
            int index = from;
            int limit = System.Math.Min(size() - from, other.Count);
            for (int i = 0; i < limit; i++)
            {
                //UPGRADE_TODO: Method 'java.util.Iterator.next' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratornext'"
                set_Renamed(index++, System.Convert.ToInt32(((System.ValueType)e.Current)));
            } //delta
        }
        /// <summary> Retains (keeps) only the elements in the receiver that are contained in the specified other list.
        /// In other words, removes from the receiver all of its elements that are not contained in the
        /// specified other list. 
        /// </summary>
        /// <param name="other">the other list to test against.
        /// </param>
        /// <returns> <code>true</code> if the receiver changed as a result of the call.
        /// </returns>
        public virtual bool retainAll(AbstractIntList other)
        {
            if (other.size() == 0)
            {
                if (size_Renamed_Field == 0)
                    return false;
                Size = 0;
                return true;
            }

            int limit = other.size() - 1;
            int j = 0;
            for (int i = 0; i < size_Renamed_Field; i++)
            {
                if (other.indexOfFromTo(getQuick(i), 0, limit) >= 0)
                    setQuick(j++, getQuick(i));
            }

            bool modified = (j != size_Renamed_Field);
            Size = j;
            return modified;
        }
        /// <summary> Reverses the elements of the receiver.
        /// Last becomes first, second last becomes second first, and so on.
        /// </summary>
        public override void reverse()
        {
            int tmp;
            int limit = size() / 2;
            int j = size() - 1;

            for (int i = 0; i < limit; )
            {
                //swap
                tmp = getQuick(i);
                setQuick(i++, getQuick(j));
                setQuick(j--, tmp);
            }
        }
        /// <summary> Replaces the element at the specified position in the receiver with the specified element.
        /// 
        /// </summary>
        /// <param name="index">index of element to replace.
        /// </param>
        /// <param name="element">element to be stored at the specified position.
        /// </param>
        /// <throws>  IndexOutOfBoundsException if <tt>index &lt; 0 || index &gt;= size()</tt>. </throws>
        public virtual void set_Renamed(int index, int element)
        {
            if (index >= size_Renamed_Field || index < 0)
                throw new System.IndexOutOfRangeException("Index: " + index + ", Size: " + size_Renamed_Field);
            setQuick(index, element);
        }
        /// <summary> Replaces the element at the specified position in the receiver with the specified element; <b>WARNING:</b> Does not check preconditions.
        /// Provided with invalid parameters this method may access invalid indexes without throwing any exception!
        /// <b>You should only use this method when you are absolutely sure that the index is within bounds.</b>
        /// Precondition (unchecked): <tt>index &gt;= 0 && index &lt; size()</tt>.
        /// 
        /// This method is normally only used internally in large loops where bounds are explicitly checked before the loop and need no be rechecked within the loop.
        /// However, when desperately, you can give this method <tt>public</tt> visibility in subclasses.
        /// 
        /// </summary>
        /// <param name="index">index of element to replace.
        /// </param>
        /// <param name="element">element to be stored at the specified position.
        /// </param>
        //UPGRADE_NOTE: Access modifiers of method 'setQuick' were changed to 'public'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1204'"
        public abstract void setQuick(int index, int element);
        /// <summary> Randomly permutes the part of the receiver between <code>from</code> (inclusive) and <code>to</code> (inclusive). </summary>
        /// <param name="from">the index of the first element (inclusive) to be permuted.
        /// </param>
        /// <param name="to">the index of the last element (inclusive) to be permuted.
        /// </param>
        /// <exception cref="IndexOutOfBoundsException">index is out of range (<tt>size()&gt;0 && (from&lt;0 || from&gt;to || to&gt;=size())</tt>).
        /// </exception>
        public override void shuffleFromTo(int from, int to)
        {
            checkRangeFromTo(from, to, size());

            System.DateTime tempAux = System.DateTime.Now;
            //UPGRADE_NOTE: ref keyword was added to struct-type parameters. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1303'"
            Uniform gen = new Uniform(new DRand(ref tempAux));
            for (int i = from; i < to; i++)
            {
                int random = gen.nextIntFromTo(i, to);

                //swap(i, random)
                int tmpElement = getQuick(random);
                setQuick(random, getQuick(i));
                setQuick(i, tmpElement);
            }
        }
        /// <summary> Returns the number of elements contained in the receiver.
        /// 
        /// </summary>
        /// <returns>s  the number of elements contained in the receiver.
        /// </returns>
        public override int size()
        {
            return size_Renamed_Field;
        }
        /// <summary> Returns a list which is a concatenation of <code>times</code> times the receiver.</summary>
        /// <param name="times">the number of times the receiver shall be copied.
        /// </param>
        public virtual AbstractIntList times(int times)
        {
            AbstractIntList newList = new IntArrayList(times * size());
            for (int i = times; --i >= 0; )
            {
                newList.addAllOfFromTo(this, 0, size() - 1);
            }
            return newList;
        }
        /// <summary> Returns a <code>java.util.ArrayList</code> containing all the elements in the receiver.</summary>
        public override System.Collections.ArrayList toList()
        {
            int mySize = size();
            System.Collections.ArrayList list = new System.Collections.ArrayList(mySize);
            for (int i = 0; i < mySize; i++)
                list.Add((System.Int32)get_Renamed(i));
            return list;
        }
        /// <summary> Returns a string representation of the receiver, containing
        /// the String representation of each element.
        /// </summary>
        public override System.String ToString()
        {
            return Arrays.toString(partFromTo(0, size() - 1).elements());
        }
        //UPGRADE_NOTE: The following method implementation was automatically added to preserve functionality. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1306'"
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }
}
